package com.mou.controller.admin;

import com.mou.annotation.Log;
import com.mou.dto.ProductDTO;
import com.mou.dto.ProductPageQueryDTO;
import com.mou.entity.Product;
import com.mou.result.PageResult;
import com.mou.result.Result;
import com.mou.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/page")
    public Result<PageResult> page(ProductPageQueryDTO productPageQueryDTO) {
        log.info("商品分页查询:{}", productPageQueryDTO);
        PageResult pageResult = productService.pageQuery(productPageQueryDTO);
        return Result.success(pageResult);
    }
    @Log
    @PutMapping("/update")
    public Result<String> update(@RequestBody ProductDTO productDTO) {
        log.info("商品修改:{}", productDTO);
        Product product = new Product();
        BeanUtils.copyProperties(productDTO, product);
        productService.updateById(product);
        return Result.success("商品修改成功");
    }

    @Log
    @PutMapping("/delete/{ids}")
    public Result<String> deleted(@PathVariable("ids") List<Integer> ids){
        log.info("商品删除:{}", ids);
        for(Integer id :ids){
            productService.removeById(id);
        }
        return Result.success();
    }
}
